package com.topsoft.email.interceptor;

import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import com.google.common.base.Strings;

public class DataInterceptor extends HandlerInterceptorAdapter {

	
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
		
		if (modelAndView!=null){
     		String viewName = modelAndView.getViewName().toUpperCase();
			if(!viewName.startsWith("redirect".toUpperCase())){
				
				//得到当前action的请求路径  
				Map<String,Object> model=modelAndView.getModelMap();
				model.put("base", request.getContextPath());
				
				//url地址
				 String basePathUrl = request.getScheme() + "://"
				               + request.getServerName() + ":"
							   + request.getServerPort()
							   + request.getContextPath() + "/";
				 model.put("base",  basePathUrl);
			}
		}
		
		
		
		//处理国际化
		String langType=request.getParameter("langType");
		if(!Strings.isNullOrEmpty(langType)){
			if(langType.equals("zh")){
	            Locale locale = new Locale("zh", "CN"); 
	            request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale); 
	        }else if(langType.equals("en")){
	            Locale locale = new Locale("en", "US"); 
	            request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale);
	        }else{ 
	            request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,LocaleContextHolder.getLocale());
	        }
			
		}
		
		
	}
}
